# -*- coding:utf-8 -*-
import socket


class Scan_Proxy:
    """
    this is a proxy to get requests or responses in Browser
    """

    def __init__(self, host='127.0.0.1', prot=9090):

        self.size = 1024
        self.addr = (host, prot)

    def Proxy_Server(self):

        """
        this is a proxy server
        """
        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server_socket.bind(self.addr)
        server_socket.listen(10)
        while True:
            try:
                print 'proxy running...'
                page, addr = server_socket.accept()
                print ">>>", addr[0], ":", addr[1]
                while True:
                    data = page.recv(self.size)
                    if not data:
                        break
                    for line in repr(data):
                        print ">>>", line
            except KeyboardInterrupt as e:
                break
                print e
                page.close()
                self.proxy.close()

    def proxy_clinet(self, data):
        """
        this is a proxy client
        """
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client_socket.connect(self.addr)
        while True:
            if not data:
                break
            client_socket.send(data)
            data = client_socket.recv(self.size)
            if not data:
                break
            print data


if __name__ == '__main__':

    sp = Scan_Proxy()
    sp.Proxy_Server()
